package com.shenyue.nbtownlet.model;

import android.content.Context;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.shenyue.nbtownlet.event.BaseListener;
import com.shenyue.nbtownlet.util.StorageUtil;
import com.shenyue.nbtownlet.util.StringUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 流程任务
 */

public class AppTaskModel {
    public static final String BTN_SAVE = "flowSave();";
    public static final String BTN_SEND = "flowSend();";
    public static final String BTN_BACK = "flowBack();";
    public static final String BTN_COMPLETED = "flowCompleted();";

    /****获取任务列表接口****/
    private static final String WAITLIST_WAITLIST = "WaitList/WaitList";
    private static final String WAITLIST_WAITLIST_ACTION = "$waitlist_waitlist_action$";

    /****获取任务详情流程相关参数配置接口****/
    private static final String WAITLIST_DETAIL = "WaitList/Detail";
    public static final String WAITLIST_DETAIL_ACTION = "$waitlist_detail_action$";
    private static final String WAITLIST_DETAIL_DATA = "$waitlist_detail_data$";

    /****流程提交****/
    private static final String WAITLIST_EXECUTE = "WaitList/Execute";
    public static final String WAITLIST_EXECUTE_ACTION = "$waitlist_execute_action$";

    /****获取流程步骤信息****/
    private static final String WAITLIST_FLOWSEND = "WaitList/FlowSend";
    public static final String WAITLIST_FLOWSEND_ACTION = "$waitlist_flowsend_action$";

    /****获取组织机构和人员接口****/
    private static final String WAITLIST_GETALLORGMEMBER = "WaitList/GetAllOrgMember";
    public static final String WAITLIST_GETALLORGMEMBER_ACTION = "$waitlist_getallorgmember_action$";

    /****流程确认完成策略****/
    private static final String WAITLIST_COMFIRMCOMPLETE = "WaitList/ComfirmComplete";
    public static final String WAITLIST_COMFIRMCOMPLETE_ACTION = "$waitlist_comfirmcomplete_action$";

    /****流程回退****/
    private static final String WAITLIST_FLOWBACK = "WaitList/FlowBack";
    public static final String WAITLIST_FLOWBACK_ACTION = "$WAITLIST_FLOWBACK_ACTION$";

    public static void WaitList(final Context context, JSONObject param, final BaseListener listener) {
        HttpClientExManager.instance().post(AppModel.kLTRootFlowURL + "/" + WAITLIST_WAITLIST, WAITLIST_WAITLIST_ACTION, param, new BaseListener() {
            @Override
            public void success(String val, String action) {
                if (listener != null)
                    listener.success(val, action);
            }

            @Override
            public void error(String error, String action) {
                if (listener != null)
                    listener.error(error, action);
            }
        });
    }

    public static void WaitListDetail(final Context context, JSONObject param, final BaseListener listener) {
        HttpClientExManager.instance().post(AppModel.kLTRootFlowURL + "/" + WAITLIST_DETAIL, WAITLIST_DETAIL_ACTION, param, new BaseListener() {
            @Override
            public void success(String val, String action) {
                if (listener != null)
                    listener.success(val, action);
            }

            @Override
            public void error(String error, String action) {
                if (listener != null)
                    listener.error(error, action);
            }
        });
    }

    public static void WaitListFlowSend(final Context context, JSONObject param, final BaseListener listener) {
        HttpClientExManager.instance().post(AppModel.kLTRootFlowURL + "/" + WAITLIST_FLOWSEND, WAITLIST_FLOWSEND_ACTION, param, new BaseListener() {
            @Override
            public void success(String val, String action) {
                if (listener != null)
                    listener.success(val, action);
            }

            @Override
            public void error(String error, String action) {
                if (listener != null)
                    listener.error(error, action);
            }
        });
    }

    public static void WaitListExecute(final Context context, JSONObject param, final BaseListener listener) {
        HttpClientExManager.instance().post(AppModel.kLTRootFlowURL + "/" + WAITLIST_EXECUTE, WAITLIST_EXECUTE_ACTION, param, new BaseListener() {
            @Override
            public void success(String val, String action) {
                if (listener != null)
                    listener.success(val, action);
            }

            @Override
            public void error(String error, String action) {
                if (listener != null)
                    listener.error(error, action);
            }
        });
    }

    public static void WaitListComfirmComplete(final Context context, JSONObject param, final BaseListener listener) {
        HttpClientExManager.instance().post(AppModel.kLTRootFlowURL + "/" + WAITLIST_COMFIRMCOMPLETE, WAITLIST_COMFIRMCOMPLETE_ACTION, param, new BaseListener() {
            @Override
            public void success(String val, String action) {
                if (listener != null)
                    listener.success(val, action);
            }

            @Override
            public void error(String error, String action) {
                if (listener != null)
                    listener.error(error, action);
            }
        });
    }

    public static void WaitListFlowBack(final Context context, JSONObject param, final BaseListener listener) {
        HttpClientExManager.instance().post(AppModel.kLTRootFlowURL + "/" + WAITLIST_FLOWBACK, WAITLIST_FLOWBACK_ACTION, param, new BaseListener() {
            @Override
            public void success(String val, String action) {
                if (listener != null)
                    listener.success(val, action);
            }

            @Override
            public void error(String error, String action) {
                if (listener != null)
                    listener.error(error, action);
            }
        });
    }

    public static void WaitListGetAllOrgMember(final Context context, JSONObject param, final BaseListener listener) {
        HttpClientExManager.instance().get(AppModel.kLTRootFlowURL + "/" + WAITLIST_GETALLORGMEMBER, WAITLIST_GETALLORGMEMBER_ACTION, param, new BaseListener() {
            @Override
            public void success(String val, String action) {
                if (listener != null)
                    listener.success(val, action);
            }

            @Override
            public void error(String error, String action) {
                if (listener != null)
                    listener.error(error, action);
            }
        });
    }

    public static List<String> getButtons(Context context, JSONObject config){
        List<String> list = new ArrayList<>();
        JSONArray buttons = config.getJSONArray("buttons");
        for (int i = 0, j = buttons.size(); i < j; i++) {
            JSONObject btn = buttons.getJSONObject(i);
            list.add(btn.getString("Script"));
        }
        return list;
    }
}
